import {getContacts} from '@/api/user';

/**
 * 账户信息
 * */
const account = {
  state: {
    contactListLoaded:false, //是否已经获取过
    contactList:[], //地址列表
  },
  mutations: {
    SET_CONTACTLIST: (state, list) => {
      state.contactListLoaded = true;
      state.contactList = list;
    },
  },
  actions: { //payload:{data:{...},reload:boolean}
    getContactList: ({commit, state}, payload) => {
      return new Promise(resolve => {
        if(payload.reload || !state.contactListLoaded) //没获取过 或者 需要强制刷新
        {
          getContacts(payload.data).then(resp=>{
            commit('SET_CONTACTLIST',resp.data.list || []);
            resolve();
          });
        }
        else
        {
          resolve();
        }
      });
    },
  },
  getters:{
    contactListLoaded: state => state.contactListLoaded,
    contactList: state => state.contactList,
  }
};

export default account;

